<template>
    <div id="app" ref="app" :style="{height: height}">
        <div v-if="netmask" class="netmask">
            <h1>请关闭网络连接...</h1>
        </div>
                <keep-alive include="Advance">
                    <router-view/>
                </keep-alive>


    </div>
</template>

<script>

    import {IO} from "./common/plus/io";
    import {md5} from '$common/md5';
    import {init} from "./common/data-base/DataInterface";

    export default {
        data() {
            return {
                netmask: false,
                t: 1000 * 10 * 6,
                inEndTimer: 0,
                height: '100%'
            }
        },
        created() {
            if (this.$production) {
                // 监听网络状态变化
                this.$EventBus.$on('DeviceNetWork', (param) => {
                    this.netmask = !(param === 1);
                });

                // PlusHasReady
                this.$EventBus.$on('PlusHasReady', () => {
                    this.$store.commit('setPlusAlready', true);

                    // 创建数据库
                    init();

                    // 创建/读取 密码文件
                    this.passwordFile();

                    // 创建/读取 关注文件
                    this.attentionFile();
                });

                // 前台到后台
                this.$EventBus.$on('FrontToEnd', () => {
                    this.inEndTimer = setTimeout(() => {
                        plus.runtime.quit();
                        window.clearTimeout(this.inEndTimer);
                    }, this.t);
                });

                // 后台到前台
                this.$EventBus.$on('EndToFront', () => {
                    window.clearTimeout(this.inEndTimer);
                });
            }
        },
        mounted() {
            if (this.$production) {
                this.height = document.documentElement.clientHeight + 'px';
                this.$router.replace('/login')
            }
        },
        destroyed() {
        },
        methods: {
            // 密码文件
            passwordFile() {
                const filename = 'password.json';
                IO.writeFile(filename, ({data, writer, error, mes}) => {
                    if (!error) {
                        let param = {
                            "superpassword": md5("123456"),
                            "devicePassword": md5("654321")
                        };
                        if (!data) {
                            writer.onwrite = function () {
                                console.log(`${filename}  创建成功`);
                            };
                            let str = JSON.stringify(param);
                            writer.seek(0);
                            writer.write(str);
                        } else {
                            param = data;
                            console.log(`${filename}  读取成功`);
                            console.log(JSON.stringify(param));
                        }
                        this.$store.commit('setDevicePasswor', param.devicePassword);
                        this.$store.commit('setSuperPasswor', param.superpassword);
                    } else {
                        alert(mes);
                    }
                });
            },
            // 关注文件
            attentionFile() {
                const filename = 'attention.json';
                IO.writeFile(filename, ({data, writer, error, mes}) => {
                    if (!error) {
                        let param = [];
                        if (!data) {
                            writer.onwrite = function () {
                                console.log(`${filename}  创建成功`);
                            };
                            let str = JSON.stringify(param);
                            writer.seek(0);
                            writer.write(str);
                        } else {
                            param = data;
                            console.log(`${filename}  读取成功`);
                            console.log(param);
                        }
                        this.$store.commit('setAttentionList', param);
                    } else {
                        alert(mes);
                    }
                });
            }
        }
    }
</script>

<style lang="scss">
    @import "./assets/scss/base";

    #app {
        height: 100%;
        width: 100%;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        text-align: center;
        color: #2c3e50;
        position: relative;

        > .netmask {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, .5);
            z-index: 1;
            display: flex;
            justify-content: center;
            align-items: center;

            h1 {
                color: #ffffff;
            }
        }
    }
</style>
